package com.shqcjd.yunweiyanoperator.ui.activity.staff;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.shqcjd.yunweiyanoperator.R;
import com.shqcjd.yunweiyanoperator.api.ApiService;
import com.shqcjd.yunweiyanoperator.helper.rxjavahelper.RxObserver;
import com.shqcjd.yunweiyanoperator.helper.rxjavahelper.RxSchedulersHelper;
import com.shqcjd.yunweiyanoperator.model.ResponseData;
import com.shqcjd.yunweiyanoperator.model.pojo.ContactsBean;
import com.shqcjd.yunweiyanoperator.model.pojo.ExpertBean;
import com.shqcjd.yunweiyanoperator.model.pojo.StaffManageBean;
import com.shqcjd.yunweiyanoperator.ui.adapter.ContactsAdapter;
import com.shqcjd.yunweiyanoperator.ui.adapter.StaffManageAdapter;
import com.shqcjd.yunweiyanoperator.ui.base.BaseActivity;
import com.shqcjd.yunweiyanoperator.ui.base.BasePresenter;
import com.shqcjd.yunweiyanoperator.util.ToastUtils;
import com.shqcjd.yunweiyanoperator.util.WrapContentLinearLayoutManager;
import com.shqcjd.yunweiyanoperator.widget.AddStaffDialog;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

/**
 * 好友列表
 */
public class ContactsActivity extends BaseActivity {


    @BindView(R.id.btn_finish)
    ImageView btnFinish;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_include_add)
    TextView tvIncludeAdd;
    @BindView(R.id.rv_contacts_manage)
    RecyclerView rvContactsManage;
    @BindView(R.id.sr_contacts_manage)
    SwipeRefreshLayout srContactsManage;
    @BindView(R.id.rl_isnull)
    RelativeLayout rlIsnull;

    private Context mContext;
    private ContactsAdapter contactsAdapter;
    private AddStaffDialog addStaffDialog;
    List<ContactsBean> mList = new ArrayList<>();
    private int type = 0;//判断是什么页面跳过来的，如果是0，则点击item的时候跳到好友详情页；如果是1，说明是路线列表页的弹窗跳进来的，点击item则finish掉当前页面

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_contacts;
    }

    @Override
    public void init() {
        super.init();
        mContext = this;
        type = getIntent().getIntExtra("type", 0);
    }

    @Override
    public void initView() {
        super.initView();
        tvTitle.setText("好友列表");
        refreshData();
        srContactsManage.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshData();
            }
        });

    }

    private void refreshData() {
        srContactsManage.setRefreshing(true);
        mList.clear();
        getDataList();
    }



    private void addStaffHttp(String name, String phone, String password, String maintain) {
        Map<String, String> map = new HashMap<>();
        map.put("maintainInfo", maintain);
        map.put("password", password);
        map.put("phoneNumber", phone);
        map.put("userName", name);
        ApiService.addStaff(new JSONObject(map).toString())
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                    }

                    @Override
                    public void _onNext(ResponseData responseData) {
                        if (responseData.retCode == 10000) {
                            refreshData();
                            ToastUtils.showShort(mContext, "添加成功");
                            addStaffDialog.dismiss();
                        } else {
                            ToastUtils.showShort(mContext, responseData.retMsg);
                        }
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        Log.e("tou", errorMessage);
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                });
    }

    private void getDataList() {
        Map<String, String> map = new HashMap<>();
        ApiService.contactsList(new JSONObject(map).toString())
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<List<ContactsBean>>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                    }

                    @Override
                    public void _onNext(ResponseData<List<ContactsBean>> responseData) {
                        if (responseData.retCode != 10000) {
                            return;
                        }
                        mList.addAll(responseData.data);
                        if (contactsAdapter == null) {
                            rvContactsManage.setLayoutManager(new WrapContentLinearLayoutManager(mContext));
                            contactsAdapter = new ContactsAdapter(mList, mContext);
                            rvContactsManage.setAdapter(contactsAdapter);
                            contactsAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                                @Override
                                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                    ContactsBean item = (ContactsBean) adapter.getItem(position);
                                    if(type==0){
                                        Intent intent = new Intent(mContext, ContactsDetailsActivity.class);
                                        intent.putExtra("isAddFriends", false);
                                        intent.putExtra("friendId", item.friendId);
                                        if ("expert".equals(item.friendAppCode)) {//专家端 id前缀加2  操作端 id前缀加1
                                            intent.putExtra("remoteId", Integer.parseInt("2"+item.friendId));
                                        } else {
                                            intent.putExtra("remoteId", Integer.parseInt("1"+item.friendId));
                                        }
                                        intent.putExtra("userName", item.friendName);
                                        intent.putExtra("phoneNumber", item.phoneNumber);
                                        intent.putExtra("businessName", item.businessName);
                                        intent.putExtra("maintainInfo", item.info);
                                        intent.putExtra("acceptAppCode", item.friendAppCode);
                                        startActivity(intent);
                                    }else if(type==1){//带返回值 关闭页面
                                        Intent intent = new Intent();
                                        intent.putExtra("frenidId", item.friendId);
                                        intent.putExtra("frenidName", item.friendName);
                                        intent.putExtra("frenidAppCode", item.friendAppCode);
                                        setResult(RESULT_OK, intent);
                                        finish();
                                    }

                                }
                            });
                        }
                        if (mList.size() == 0) {
                            rlIsnull.setVisibility(View.VISIBLE);
                        } else {
                            rlIsnull.setVisibility(View.INVISIBLE);
                        }
                        contactsAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        Log.e("tou", errorMessage);
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        srContactsManage.setRefreshing(false);
                    }
                });
    }


    @OnClick({R.id.btn_finish, R.id.tv_include_add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_finish:
                finish();
                break;
//            case R.id.tv_include_add:
//                if(addStaffDialog==null){
//                    addStaffDialog = new AddStaffDialog(mContext);
//                    addStaffDialog.setOnClickDialogListener(new AddStaffDialog.setOnClickDialogListener() {
//                        @Override
//                        public void onClickAdd(String name, String phone, String password, String maintain) {
//                            addStaffHttp(name, phone, password, maintain);
//                        }
//
//                        @Override
//                        public void onAddDepartment() {
//                        }
//
//                        @Override
//                        public void onSpecialty() {
//
//                        }
//                    });
//
//                }
//                if(!addStaffDialog.isShowing()){
//                    addStaffDialog.show();
//                }
//                break;
        }
    }
}
